﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SomeRandomGame.Interfaces;

namespace SomeRandomGame.Graphics
{
    public class FrameBasedAnimation: IAnimation
    {
        private Texture2D _spriteStrip;
        private float _scale;
        private int _elapsedTime;
        private int _frameTime;
        private int _frameCount;
        private int _currentFrame;
        private Color _color;
        private Rectangle _sourceRect;
        private Rectangle _destinationRect;
        private int _frameWidth;
        private int _frameHeight;
        private bool _active;
        private bool _looping;
        private Vector2 _position;

        public FrameBasedAnimation( Texture2D texture, Vector2 position,
                                    int frameWidth, int frameHeight, int frameCount,
                                    int frametime, Color color, float scale, bool looping )
        {
            _color = color;
            _frameWidth = frameWidth;
            _frameHeight = frameHeight;
            _frameCount = frameCount;
            _frameTime = frametime;
            _scale = scale;


            _looping = looping;
            _position = position;
            _spriteStrip = texture;


            // Set the time to zero
            _elapsedTime = 0;
            _currentFrame = 0;


            // Set the FrameBasedAnimation to active by default
            _active = true;
        }

        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }

        public int Width
        {
            get { return _frameWidth; }
        }

        public int Height
        {
            get { return _frameHeight; }
        }

        public bool Active
        {
            get { return _active; }
        }

        public bool Looping
        {
            get { return _looping; }
        }

        public void Initialize( IGameManager gameManager )
        {
            // Nothing to do
        }

        public void Update( IGameManager gameManager )
        {
            if( !_active )
            {
                return;
            }

            _elapsedTime += (int)gameManager.GameTime.ElapsedGameTime.TotalMilliseconds;

            if( _elapsedTime > _frameTime )
            {
                _currentFrame++;

                if( _currentFrame == _frameCount )
                {
                    _currentFrame = 0;

                    if( !_looping )
                    {
                        _active = false;
                    }
                }

                _elapsedTime = 0;
            }

            _sourceRect = new Rectangle( _currentFrame * _frameWidth, 0, _frameWidth, _frameHeight );
            _destinationRect = new Rectangle( (int)_position.X - (int)( _frameWidth * _scale ) / 2,
                                              (int)_position.Y - (int)( _frameHeight * _scale ) / 2,
                                              (int)( _frameWidth * _scale ),
                                              (int)( _frameHeight * _scale ) );
        }

        public void Draw( SpriteBatch spriteBatch, IGameManager gameManager, GameTime gameTime )
        {
            if( _active )
            {
                spriteBatch.Begin();
                spriteBatch.Draw( _spriteStrip, _destinationRect, _sourceRect, _color );
                spriteBatch.End();
            }
        }
    }
}
